package com.czz.ucenter.controller;

import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.ucenter.config.JwtProperties;
import com.czz.ucenter.entity.Teacher;
import com.czz.ucenter.entity.User;
import com.czz.ucenter.service.TeacherService;
import com.czz.ucenter.service.UserService;
import com.czz.ucenter.utils.CookieUtils;
import com.czz.ucenter.utils.JwtUtils;
import com.czz.ucenter.vo.TeacherInfoVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: TeacherController
 * @Author Czz
 * @Date 2019/12/23
 * @Time 16:35
 **/
@RestController
@RequestMapping(value = "/teacher/teachers")
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @Autowired
    UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    static final String TOKEN_PREFIX = "teacher:token:";
    /**
    * @Description 讲师登录
    * @Param [username, password, request, response]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2019/12/24
    * @Time 10:53
    **/
    @PostMapping(value = "/login")
    public R login(@RequestParam String username,
                   @RequestParam String password,
                   HttpServletRequest request,
                   HttpServletResponse response){

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }

        //查询用户
        User user = userService.login(username,password,true);

        if (ObjectUtils.isEmpty(user)){
            return R.setResult(ResultCodeEnum.USERNAME_OR_PASSWORD_ERROR);
        }

        //查询讲师
        Teacher teacher = teacherService.getTeacherByUserId(user.getId());

        if (ObjectUtils.isEmpty(teacher)){
            return R.setResult(ResultCodeEnum.USERNAME_OR_PASSWORD_ERROR);
        }

        String token = JwtUtils.generateJWT(user,teacher);
        //存入cookies,token和cookies有效期都为60分钟
        //CookieUtils.setCookie(request,response, JwtProperties.TEACHERNAME,token,JwtProperties.MANAGEEXPIRE*60,null,false);

        //token存入redis，管理员过期时间120分钟，2小时
        this.redisTemplate.opsForValue().set(TOKEN_PREFIX + teacher.getTeacherID(), token, JwtProperties.MANAGEEXPIRE, TimeUnit.MINUTES);
        return R.ok().data("token",token);
    }
    
    /**
    * @Description 根据token解析讲师信息
    * @Param [token]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2019/12/24
    * @Time 11:20
    **/
    @GetMapping(value = "/verify")
    public R getAdminInfoByJwt(HttpServletRequest request){
        String token = request.getHeader("TEACHER-TOKEN");

        if (StringUtils.isEmpty(token)){
            return R.setResult(ResultCodeEnum.UNAUTHORIZED);
        }

        Claims claims = JwtUtils.checkJWT(token);
        String id = (String) claims.get("id");
        String mobile = (String) claims.get("mobile");
        String name = (String) claims.get("name");
        String avatar = (String) claims.get("avatar");

        TeacherInfoVo teacherInfoVo = new TeacherInfoVo();
        teacherInfoVo.setId(id);
        teacherInfoVo.setMobile(mobile);
        teacherInfoVo.setName(name);
        teacherInfoVo.setAvatar(avatar);

        String redisToken = this.redisTemplate.opsForValue().get(TOKEN_PREFIX + id);

        if (ObjectUtils.isEmpty(teacherInfoVo) || StringUtils.isEmpty(redisToken) || !token.equals(redisToken)){
            return R.setResult(ResultCodeEnum.UNAUTHORIZED);
        }
        return R.ok().data("userInfo",teacherInfoVo);
    }

    /**
    * @Description 讲师注销
    * @Param [teacherId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/3/21
    * @Time 18:48
    **/
    @PostMapping(value = "/logout")
    public R logout(String teacherId){
        if (StringUtils.isEmpty(teacherId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        //删除redis中token
        this.redisTemplate.delete(TOKEN_PREFIX + teacherId);
        return R.ok();
    }
}
